<template>
	<div>
		<el-row :gutter="20">
			<el-col :span="8">
				<el-card shadow="hover" class="mgb20" style="height: 252px">
					<div class="user-info">
						<el-avatar :size="120" :src="imgurl" />
						<div class="user-info-cont">
							<div class="user-info-name">{{PlatformInfo.username}}</div>
							<div>超级管理员</div>
						</div>
					</div>
					<div class="user-info-list">
						上次登录时间：
						<span>{{PlatformInfo.login_time}}</span>
					</div>
				</el-card>
			</el-col>
			<el-col :span="16">
				<el-row :gutter="20" class="mgb20">
					<el-col :span="8">
						<el-card shadow="hover" :body-style="{ padding: '0px' }">
							<div class="grid-content grid-con-1">
								<el-icon class="grid-con-icon"><User /></el-icon>
								<div class="grid-cont-right">
									<div class="grid-num">{{PlatformInfo.all_users_count}}</div>
									<div>平台用户总量</div>
								</div>
							</div>
						</el-card>
					</el-col>

					<el-col :span="8">
						<el-card shadow="hover" :body-style="{ padding: '0px' }">
							<div class="grid-content grid-con-2">
                <el-icon class="grid-con-icon"><QuestionFilled /></el-icon>
								<div class="grid-cont-right">
									<div class="grid-num">{{PlatformInfo.all_questions_count}}</div>
									<div>平台问题总量</div>
								</div>
							</div>
						</el-card>
					</el-col>

					<el-col :span="8">
						<el-card shadow="hover" :body-style="{ padding: '0px' }">
							<div class="grid-content grid-con-3">
                <el-icon class="grid-con-icon"><EditPen /></el-icon>
								<div class="grid-cont-right">
									<div class="grid-num">{{PlatformInfo.all_answers_count}}</div>
									<div>平台回答总量</div>
								</div>
							</div>
						</el-card>
					</el-col>

          <el-col :span="8" class="mgt">
            <el-card shadow="hover" :body-style="{ padding: '0px' }">
              <div class="grid-content grid-con-1">
                <el-icon class="grid-con-icon"><UserFilled /></el-icon>
                <div class="grid-cont-right">
                  <div class="grid-num">{{PlatformInfo.today_users_count}}</div>
                  <div>今日新增用户量</div>
                </div>
              </div>
            </el-card>
          </el-col>

          <el-col :span="8" class="mgt">
            <el-card shadow="hover" :body-style="{ padding: '0px' }">
              <div class="grid-content grid-con-2">
                <el-icon class="grid-con-icon"><QuestionFilled /></el-icon>
                <div class="grid-cont-right">
                  <div class="grid-num">{{PlatformInfo.today_questions_count}}</div>
                  <div>今日新增问题量</div>
                </div>
              </div>
            </el-card>
          </el-col>

          <el-col :span="8" class="mgt">
            <el-card shadow="hover" :body-style="{ padding: '0px' }">
              <div class="grid-content grid-con-3">
                <el-icon class="grid-con-icon"><Edit /></el-icon>
                <div class="grid-cont-right">
                  <div class="grid-num">{{PlatformInfo.today_answers_count}}</div>
                  <div>今日新增回答量</div>
                </div>
              </div>
            </el-card>
          </el-col>
				</el-row>
			</el-col>
		</el-row>

		<el-row :gutter="20">

			<el-col :span="12">
				<el-card shadow="hover">
					<schart ref="bar" class="schart" canvasId="bar" :options="options1"></schart>
				</el-card>
			</el-col>

			<el-col :span="12">
				<el-card shadow="hover" class="RightBox">
					<schart ref="pie" class="schart" canvasId="pie" :options="options2"></schart>
				</el-card>
			</el-col>
		</el-row>
	</div>
</template>

<script setup name="dashboard">
import Schart from 'vue-schart';
import imgurl from '../../assets/img/img.jpg';
import { useRouter } from "vue-router";
import {ref, watch} from "vue";
import axios from "axios";

const dateArr=ref([]),QuestionNum=ref([]),AnswerNum=ref([]),UserNum=ref([]),TopicName=ref([]),TopicNum=ref([])
const options1 = {
  type: 'bar',
  title: {
    text: '近一周平台新增用户数量概览'
  },
  labels: dateArr.value,
  datasets: [
    {
      label: '用户数量',
      data: UserNum.value
    },
    {
      label: '问题数量',
      data: QuestionNum.value
    },
    {
      label: '回答数量',
      data: AnswerNum.value
    }
  ]
};
const options2 = {
  type: 'pie',
  title: {
    text: '话题内容数量概览'
  },
  legend: {
    position: 'left'
  },
  bgColor: '#fbfbfb',
  labels: TopicName.value,
  datasets: [{
    data: TopicNum.value
  }]
};
const PlatformInfo = ref({})
const router = useRouter()
const getData = ()=>{
  axios.get(`http://localhost:8080/admin/info`)
      .then(res=>{
        PlatformInfo.value=res.data.data
        console.log(PlatformInfo.value)
      })
  axios.get(`http://localhost:8080/admin/dayInfo`)
      .then(res=>{
        let data=res.data.data
        for(let i=0;i<data.length;i++){
          dateArr.value.push(data[i].monthAndDay)
          QuestionNum.value.push(data[i].new_questions_count)
          AnswerNum.value.push(data[i].new_answers_count)
          UserNum.value.push(data[i].new_users_count)
        }
      })
  axios.get(`http://localhost:8080/admin/topic`)
      .then(res=>{
        let data=res.data.data
        for(let i=0;i<data.length;i++){
          TopicName.value.push(data[i].topic_name)
          TopicNum.value.push(data[i].count)
        }
        console.log(TopicName,TopicNum)
      })
}
getData()
</script>

<style scoped>

.grid-content {
	display: flex;
	align-items: center;
	height: 100px;
}

.grid-cont-right {
	flex: 1;
	text-align: center;
	font-size: 14px;
	color: #999;
}

.grid-num {
	font-size: 30px;
	font-weight: bold;
}

.grid-con-icon {
	font-size: 50px;
	width: 100px;
	height: 100px;
	text-align: center;
	line-height: 100px;
	color: #fff;
}

.grid-con-1 .grid-con-icon {
	background: rgb(45, 140, 240);
}

.grid-con-1 .grid-num {
	color: rgb(45, 140, 240);
}

.grid-con-2 .grid-con-icon {
	background: rgb(100, 213, 114);
}

.grid-con-2 .grid-num {
	color: rgb(100, 213, 114);
}

.grid-con-3 .grid-con-icon {
	background: rgb(242, 94, 67);
}

.grid-con-3 .grid-num {
	color: rgb(242, 94, 67);
}

.user-info {
	display: flex;
	align-items: center;
	padding-bottom: 20px;
	border-bottom: 2px solid #ccc;
	margin-bottom: 20px;
}

.user-info-cont {
	padding-left: 50px;
	flex: 1;
	font-size: 14px;
	color: #999;
}

.user-info-cont div:first-child {
	font-size: 30px;
	color: #222;
}

.user-info-list {
  margin-top: 35px;
  margin-left: 10px;
	font-size: 14px;
	color: #999;
	line-height: 25px;
}

.user-info-list span {
	margin-left: 70px;
}

.mgb20 {
	margin-bottom: 20px;
}

.mgt {
  margin-top: 49px;
}

.schart {
	width: 100%;
	height: 300px;
}
.RightBox {
  padding-left: 120px;
}
</style>
